/**
 * 
 */
package com.ydy.service.shop.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydy.constant.SystemConstant;
import com.ydy.exception.BusinessException;
import com.ydy.exception.DataNotFoundException;
import com.ydy.exception.ValidateException;
import com.ydy.ienum.EnumShop;
import com.ydy.mapper.ShopMapper;
import com.ydy.model.Shop;
import com.ydy.model.Staff;
import com.ydy.service.shop.ShopService;
import com.ydy.utils.ValidateUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;
import com.ydy.vo.other.ResultVo;

/**
 * @author xuzhaojie
 *
 *         2019年5月29日 上午8:35:26
 */
@Service
@Transactional
public class ShopServiceImpl implements ShopService {
	private final static Logger log = LoggerFactory.getLogger(ShopServiceImpl.class);
	@Autowired
	private ShopMapper shopMapper;

	@Override
	public PageVo<Shop> select(Shop shop, Integer page, Integer size) {
		PageVo<Shop> vo = new PageVo<Shop>(page, size);
		Page<Shop> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "id desc");
		List<Shop> list = shopMapper.select(shop);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public PageVo<Shop> list(Shop shop, Integer page, Integer size) {
		PageVo<Shop> vo = new PageVo<Shop>(page, size);
		Page<Shop> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "id desc");
		shop.setOnStatus(SystemConstant.USE_STATUS_ON);
		List<Shop> list = shopMapper.select(shop);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public Shop saveOrUpdate(Shop shop, Staff staff) {
		if (shop == null) {
			throw new NullPointerException();
		}
		// 校验数据有效
		Map<String, String> validateInfo = ValidateUtil.validateEntity(shop);
		if (!validateInfo.isEmpty()) {
			throw new ValidateException(validateInfo);
		}
		if (staff != null) {
			shop.setId(staff.getShopId());
		}
		// 新增信息
		if (shop.getId() == null) {
			shop.setOnStatus(SystemConstant.USE_STATUS_OFF);
			shop.setCreateTime(new Date());
			shopMapper.insertSelective(shop);
			log.info("新增shop成功：" + shop.getId());
		} else {// 根据id更新信息
			Shop temp = shopMapper.selectByPrimaryKey(shop.getId());
			if (temp == null) {
				log.info("找不到shop信息：" + shop.getId());
				throw new DataNotFoundException(EnumShop.DATA_NOT_FOUND);
			}
			shopMapper.updateByPrimaryKeySelective(shop);
			shop.setCreateTime(temp.getCreateTime());
			shop.setOnStatus(temp.getOnStatus());
			log.info("更新shop成功：" + shop.getId());
		}
		return shop;
	}

	@Override
	public BaseVo delete(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Shop temp = shopMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到shop信息：" + id);
			throw new DataNotFoundException(EnumShop.DATA_NOT_FOUND);
		}
		shopMapper.deleteByPrimaryKey(id);
		log.info("删除shop成功：" + id);
		return new ResultVo();
	}

	@Override
	public BaseVo status(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Shop temp = shopMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到shop信息：" + id);
			throw new DataNotFoundException(EnumShop.DATA_NOT_FOUND);
		}
		Shop shop = new Shop();
		shop.setId(id);
		StringBuilder builder = new StringBuilder();
		if (Objects.equals(SystemConstant.USE_STATUS_ON, temp.getOnStatus())) {
			shop.setOnStatus(SystemConstant.USE_STATUS_OFF);
			builder.append("Shop下架成功:").append(id);
		} else {
			shop.setOnStatus(SystemConstant.USE_STATUS_ON);
			builder.append("Shop上架成功:").append(id);
		}
		shopMapper.updateByPrimaryKeySelective(shop);
		log.info(builder.toString());
		return new ResultVo();
	}

	@Override
	public Shop selectById(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Shop temp = shopMapper.selectByPrimaryKey(id);
		if (temp == null) {
			throw new BusinessException(EnumShop.DATA_NOT_FOUND);
		}
		return temp;
	}

}
